const app = getApp()
const util = require('../../../utils/util.js')
const http = require('../../../utils/http.js');
Page({
  data: {
    Tname: '喜赠',
    recInfo: {
      number: '1584445588752',
      headimgurl: 'http://img2.imgtn.bdimg.com/it/u=3230300394,3531999720&fm=26&gp=0.jpg',
      nickname: '-   余温绕梁'
    },
    showInfo: true,
    referee_data: '',
    client_data: '',
    statusd: 1
  },
  onLoad: function (options) {
    console.log(options.q)
    var _this = this,
      data = {
        number: options.q.substring(options.q.length - 13)
        // number:1589945768382
      }
    _this.getRefereeInfo(data)
  },
  onReady: function () {},
  onShow: function () {
    wx.login({})
  },
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {},
  /**
   * 获取推荐人信息
   */
  //获取推荐人信息
  getRefereeInfo(data) {
    var _this = this,
      recInfo = _this.data.recInfo
    http.postRequest('v1/getRefereeInfo', data,
      function (res) {
        recInfo.number = data.number
        recInfo.headimgurl = res.data.avatar_url
        recInfo.nickname = res.data.nickname
        _this.setData({
          recInfo: recInfo,
          referee_data: JSON.stringify(res.data),
          showInfo: false
        })
        setTimeout(() => {
          _this.setData({
            statusd: 0
          })
        }, 1200);
      },
      function (res) {
        // _this.toastDialog.showDialog(res.msg)
        wx.showToast({
          
          title: res.msg,
          icon: 'none'
        })
        setTimeout(() => {
          _this.setData({
            statusd: 0
          })
        }, 1200);
      })
  },
  //跳转输入邀请码
  bindGetUserInfo: util.throttle(function (e) {
    wx.showToast({
      title: '登陆中...',
      icon: 'loading'
    })
    wx.login({
      success: res => {
        const code = res.code;
        const userInfo = e.detail;
        var stringInfo = JSON.stringify(userInfo)
        var info = stringInfo.split(''),
          list = ''
        for (let i in info) {
          if (info[i] != ' ') {
            list = list + info[i]
          }
        }
        const data = {
          client_data: list,
          code: code
        }
        if (e.detail.userInfo) {
          this.wxLogin(data)
          wx.setStorageSync('Info', JSON.parse(list))
          this.setData({
            client_data: JSON.parse(list)
          })
        } else {
          wx.showModal({
            title: '温馨提示',
            content: '请授权'
          })
        }
      }
    })
  }, 1000),
  //授权登录
  wxLogin(data) {
    var _this = this
    http.postRequest('v1/wxLogin', data,
      function (res) {
        wx.setStorageSync('token', res.data.token)
        getApp().globalData.token = res.data.token;
        if (res.code == 200||res.code == 201) {
          _this.syncData({is_sync:1})
        }
      },
      function (res) {
        if (res.code == 402) {
          _this.Go()
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
  },
  //缓存数据
  syncData(data) {
    var _this = this
    http.postRequest('syncData', data,
      function (res) {
        wx.setStorageSync('member', res.data)
        getApp().globalData.token = res.data.token;
        if (res.code == 200) {
          console.log(res)
          if (res.data.member_info != undefined) {
            wx.setStorageSync('renzheng', 1)
          }
          wx.showToast({
            title: '登陆成功',
            icon: 'success'
          })
          setTimeout(() => {
            wx.switchTab({
              url: '/tabbarPage/index/index',
            })
          }, 1500)
        }
      },
      function (res) {
        if (res.code == 402) {
          _this.Go()
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
  },
  Go: util.throttle(function (e) {
    if (this.data.showInfo) {
      this.toastDialog.showDialog('请填写正确的邀请码!')
    } else {
      wx.showToast({
        title: '加载中',
        icon: 'loading'
      })
      wx.login({
        success: res => {
          const data = {
            client_data: JSON.stringify(this.data.client_data),
            code: res.code,
            referee_data: this.data.referee_data
          }
          this.wxLoginNumber(data)
        }
      })
    }
  }, 1000),
  //授权登录
  wxLoginNumber(data) {
    var _this = this
    http.postRequest('v1/wxRegister', data,
      function (res) {
        getApp().globalData.token = res.data.token;
        wx.setStorageSync("token", res.data.token)
        _this.syncDataNumber({is_sync:1})
      },
      function (res) {
        // _this.toastDialog.showDialog(res.msg)
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      })
  },
  //缓存数据
  syncDataNumber(data) {
    http.postRequest('syncData', data,
      function (res) {
        wx.setStorageSync('member', res.data)
        getApp().globalData.token = res.data.token;
        if (res.code == 200) {
          console.log(res)
          if (res.data.member_info != undefined) {
            wx.setStorageSync('renzheng', 1)
          }
          wx.showToast({
            title: res.msg,
            icon: 'success'
          })
          setTimeout(() => {
            wx.switchTab({
              url: '/tabbarPage/index/index',
            })
          }, 1500)
        }
      },
      function (res) {})
  },
  back(e) {
    wx.switchTab({
      url: '/tabbarPage/index/index',
    })
  }
})